package com.two.shopping.mapper;

import com.two.shopping.pojo.Commodity;
import com.two.shopping.pojo.Orders;
import com.two.shopping.pojo.ShoppingCart;
import com.two.shopping.pojo.User;
import org.apache.ibatis.annotations.*;

import java.util.List;


//这是购物车的Mapper模块
@Mapper
public interface IShoppingCartMapper {
    //这是向购物车里添加购买信息的方法
    @Insert("insert into shopping_cart (user_id,commodity_id,quantity,create_time,updata_time,price,commodity_name)" +
            " values(#{userId},#{commodityId},#{quantity},#{createTime},#{updateTime},#{price},#{commodityName})")
    void addCart(ShoppingCart shoppingCart);

    //全部查询购物车里的商品
    @Select("select *from shopping_cart where user_id=#{id}")
    List<ShoppingCart> findAll(User user);

    //这是从购物车删除的方法,参数是id(结算购物车中为id=#{id}的商品)
    @Delete("delete  from shopping_cart where id=#{id}")
    void deleteCartId(ShoppingCart shoppingCart);

    //向Order订单表中插入记录，与deleteCartId连用，达到从购物车删除转移至订单表
    @Insert("insert  into orders (user_id,commodity_id,quantity,price,state,create_time,update_time" +
            ") values(#{userId},#{commodityId},#{quantity},#{price},1,#{createTime},#{updateTime})")
    void shoppingCartIdOkOrders(Orders orders);

    //根据id进行查询
    @Select("select * from shopping_cart where id=#{id}")
    ShoppingCart findId(ShoppingCart shoppingCart);

    //对应购买商品则会从数据库扣除
    @Update("update commodity set quantity=#{quantity} where id=#{id}")
    void minus(Commodity commodity);


}
